package org.example.taskmaster.fragments;

import java.util.ArrayList;

import org.example.taskmaster.R;
import org.example.taskmaster.database.DatabaseAdapter;
import org.example.taskmaster.activities.MainActivity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemLongClickListener;

public class ListSelectFragment extends DetailsFragment implements OnClickListener{
	
	// references to views
	private ListView mListView;
	private Button mNewList;
	private EditText mNewListName;

	public ListSelectFragment() {
		type = "ListSelect";
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		if (container == null) {
			return null;
		}
		// create the View from the XML layout
		View mainView = inflater.inflate(R.layout.listselect_fragment_layout, container, false);

        // get reference to database
        dbAdapter = new DatabaseAdapter(getActivity());
        dbAdapter.open();

        // get references to views
		mListView = (ListView) mainView.findViewById(R.id.listselect_list);
		mNewList = (Button) mainView.findViewById(R.id.listselect_newlist);	// the CreateList button
		mNewListName = (EditText) mainView.findViewById(R.id.listselect_newlistname); // the New List Name text entry field

		// registers self as listener for the New List button
		mNewList.setOnClickListener(this);

		// get lists from database and display them
		if (mListView != null) {
			this.populate();
		}
        
        // set listener for List View
        mListView.setOnItemClickListener(new OnItemClickListener() {
        	public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        		CharSequence CSlist = ((TextView) view).getText();
        		String list = CSlist.toString(); 
        		
        		// refer to MainActivity to load a task list into details area
        		MainActivity main = (MainActivity) getActivity();
        		main.showTaskList(list);
        	}
        });
        mListView.setOnItemLongClickListener(new OnItemLongClickListener() {
        	public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
        		CharSequence CSlist = ((TextView) view).getText();
        		String list = CSlist.toString(); 

        		Toast.makeText(getActivity().getApplicationContext(), "Deleting List:"+list, Toast.LENGTH_SHORT).show();
        		// delete the list
        		dbAdapter.deleteList(list);
        		
        		// refer to MainActivity to load a task list into details area
        		MainActivity main = (MainActivity) getActivity();
        		main.showListSelect();
        		return true;
        	}
        });

	
		return mainView;
	}
	
	@Override
	public void onClick(View v) {
    	switch (v.getId()) {
		case R.id.listselect_newlist:
			String name = mNewListName.getText().toString().trim();
			long id = dbAdapter.createList(name);
			// Respond to invalid name for new list, or refresh ListView
			if (id == -1) {Toast.makeText(getActivity().getApplicationContext(), "List name must not be empty.", Toast.LENGTH_SHORT).show();}
			if (id == -2) {Toast.makeText(getActivity().getApplicationContext(), "Duplicate list name.", Toast.LENGTH_SHORT).show();}
			else {this.populate();}
			// Clear New-List-Name text field
			mNewListName.setText( "");
			break;
		}
	}

	private void populate() {
        // generate array of String representations of list names
		cursor = dbAdapter.fetchAllLists();
        ArrayList<String> lists = new ArrayList<String>();
        cursor.moveToFirst();
        while (cursor.isAfterLast() == false) {
        	lists.add(cursor.getString(1) );
        	cursor.moveToNext();
        }
        String[] task_list = lists.toArray(new String[lists.size()]);
        
        // set content of List View
        if (task_list == null) {
        	task_list = new String[] {"no", "null", "pointer","today"};
        }
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1, task_list);
        mListView.setAdapter(adapter);
	}


}
